package com.xinghen.cors;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.servlet.config.annotation.CorsRegistration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * @author xinghen
 * @ClassName: CorsConfig
 * @Package: com.xinghen.cors
 * @Description CorsConfig ( another way @see EndpointCorsProperties.java )
 * @create 2017-12-08 15:04
 */
@Configuration
@ConfigurationProperties(prefix = "com.xinghen.cors")
@Validated
@Data
public class CorsConfig {

    /**
     * 跨域信息
     */
    @NotNull
    private Map<String, CorsRegistrationConfig> config;

    /**
     * corsConfigurer
     *
     * @return
     */
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                //扫描地址
                if (!CollectionUtils.isEmpty(config)) {
                    config.forEach((key, item) -> {
                        CorsRegistration cr = registry.addMapping(item.getMapping());
                        if (item.getAllowCredentials() != null) {
                            cr.allowCredentials(item.getAllowCredentials());
                        }
                        if (StringUtils.isNotBlank(item.getAllowedOrigins())) {
                            String[] allowedOrigins = item.getAllowedOrigins().split(",");
                            cr.allowedOrigins(allowedOrigins);
                        }
                        if (StringUtils.isNotBlank(item.getAllowedMethods())) {
                            String[] allowedMethods = item.getAllowedMethods().split(",");
                            cr.allowedOrigins(allowedMethods);
                        }
                        if (StringUtils.isNotBlank(item.getAllowedHeaders())) {
                            String[] allowedHeaders = item.getAllowedHeaders().split(",");
                            cr.allowedOrigins(allowedHeaders);
                        }
                    });
                }
            }
        };
    }


    @Data
    public static class CorsRegistrationConfig {

        /**
         * 扫描地址
         */
        private String mapping = "/**";

        /**
         * 允许cookie
         */
        private Boolean allowCredentials = true;

        /**
         * 允许的域
         */
        private String allowedOrigins = "*";

        /**
         * 允许的方法
         */
        private String allowedMethods = "POST,GET,DELETE,PUT";

        /**
         * 允许的头信息
         */
        private String allowedHeaders = "*";

    }

}
